package utils

import (
	"bytes"
	"math/rand"
	"os"
	"os/user"
	"strconv"
	"time"

	svg "github.com/ajstarks/svgo"
)

func GenerateSVG(width, height int) ([]byte, string) {
	rand.Seed(time.Now().UnixNano())

	var svgContent bytes.Buffer
	canvas := svg.New(&svgContent)
	canvas.Start(width, height)
	canvas.Rect(0, 0, width, height, "fill:white")
	canvas.Circle(width/2, height/2, width/2-5, "fill:#EEE")

	code := strconv.Itoa(rand.Intn(9999))
	canvas.Text(width/2, height/2, code, "text-anchor:middle; font-size:40px; fill:black;")

	canvas.End()

	return svgContent.Bytes(), code
}

func GetConfigDir() string {
	// 获取配置文件夹路径路径
	userInfo, err := user.Current()
	if err != nil {
		panic("配置文件夹路径获取失败" + err.Error())
	}
	var homeDir = userInfo.HomeDir
	// 判断 homeDir/GTools 文件夹是否存在
	var gtDir = homeDir + "/go-code/myproject"
	_, err = os.Stat(gtDir)
	if err != nil {
		if os.IsNotExist(err) {
			err = os.Mkdir(gtDir, os.ModePerm)
			if err != nil {
				panic("配置文件夹创建失败")
			}
		} else {
			panic("配置文件夹不存在--" + err.Error())
		}
	}
	return gtDir
}
